package com.sc.VenusQueryBeta.presenter.account.xiaofei;


import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.sc.VenusQueryBeta.contract.account.xiaofei.XiaoFei02Contract;
import com.sc.VenusQueryBeta.model.account.xiaofei.XiaoFei2Model;
import com.sc.VenusQueryBeta.model.bean.account.CurrencyListBean;
import com.sc.VenusQueryBeta.model.bean.account.xiaofei.XiaoFei02Bean;

import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 19/10/2018.
 */

public class XiaoFei2Presenter extends XiaoFei02Contract.XiaoFei2Presenter {
    private int mCount = 10;
    private int mStart;
    private boolean isLoading;

    public static XiaoFei2Presenter newInstance() {
        return new XiaoFei2Presenter();
    }

    @Override
    protected XiaoFei02Contract.IXiaoFei2Model getModel() {
        return XiaoFei2Model.newInstance();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void queryAllCurrencyList(String serverAddress, String projectName, int serverPort) {
        if (mIView == null || mIModel == null)
            return;
        mRxManager.register(mIModel.queryAllCurrencyList(serverAddress, projectName, serverPort).subscribe(new Consumer<CurrencyListBean>() {
            @Override
            public void accept(CurrencyListBean currencyListBean) throws Exception {
                mIView.showqueryAllCurrencyList(currencyListBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtils.e("Network error." + throwable.getMessage());
//                if (mIView != null)
//                    return;
                mIView.showToast("Network error.");
                mIView.showNetworkError(throwable);
            }
        }));
    }

    @Override
    public void getXiaoFei2(String serverAddress, String projectName, int serverPort, int clientindex, String clientAppmsgnum, String roomth, String roomyear, int roomid, String currency) {
        if (roomth.equals("全年")) {//是全年 参数要变
            roomth = "";
        }
        if (mIView == null || mIModel == null)
            return;
        mStart = 0;
        mRxManager.register(mIModel.getXiaoFei2(serverAddress, projectName, serverPort, clientindex, mStart, mCount, clientAppmsgnum, roomth, roomyear, roomid,currency).subscribe(new Consumer<XiaoFei02Bean>() {
            @Override
            public void accept(XiaoFei02Bean xiaoFei02Bean) throws Exception {
                if (mIView != null)
                    mStart += mCount;
                mIView.showXiaoFei01Data1Success(xiaoFei02Bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
//                    if (mIView.isVisiable())
                    LogUtils.e("第一次加載 error==" + throwable.getMessage());
//                    mIView.showToast("Network error");
                    mIView.showNetworkError(throwable);
                }
            }
        }));
    }

    @Override
    public void getMoreXiaoFei2(String serverAddress, String projectName, int serverPort, int clientindex, String clientAppmsgnum, String roomth, String roomyear, int roomid, String currency) {
        if (roomth.equals("全年")) {//是全年 参数要变
            roomth = "";
        }
        if (mIView == null || mIModel == null)
            return;
        if (!isLoading) {
            isLoading = true;
            //一次加载 条数据
            mRxManager.register(mIModel.getXiaoFei2(serverAddress, projectName, serverPort, clientindex, mStart, mCount, clientAppmsgnum, roomth, roomyear, roomid, currency).subscribe(new Consumer<XiaoFei02Bean>() {
                @Override
                public void accept(XiaoFei02Bean xiaoFei02Bean) throws Exception {
                    isLoading = false;
                    if (mIView == null)
                        return;
                    if (xiaoFei02Bean != null && xiaoFei02Bean.getList() != null && xiaoFei02Bean.getList().size() > 0) {
                        mStart += mCount;
                        LogUtils.e("mStart1==" + mStart);
                        mIView.showXiaoFei01Data1Success(xiaoFei02Bean);
                        //缓存数据

                    } else {
                        //否则显示没有加载更多数据
                        mIView.showNoMoreData();
                    }

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading = false;
                    if (mIView != null) {
//                    if (mIView.isVisiable())
                        LogUtils.e("加載更多 error==" + throwable.getMessage());
                        mIView.showToast("Network error");
                        mIView.showNetworkError(throwable);
                    }
                }
            }));
        }
    }
}
